import dayjs from 'dayjs'
import utc from 'dayjs/plugin/utc'
import customParseFormat from 'dayjs/plugin/customParseFormat'
import 'dayjs/locale/zh-cn' // import locale

dayjs.extend(utc)
dayjs.extend(customParseFormat)
dayjs.locale('zh-cn') // use locale

const time = Object.create(null)

// 获取dayjs实例
time.getDayJs = function() {
  return dayjs
}

// 获取今天时间戳
time.getTodayTimeStamp = function() {
  return dayjs().valueOf()
}

// 获取时间转换时间戳
time.getCurrectTimeStamp = function(date) {
  return dayjs(date).valueOf()
}

// 获取当前时间UTC
time.getTodayUTC = function() {
  return dayjs()
    .utc()
    .format()
}

// 日期转换UTC
time.formatDateToUTC = function(date) {
  return dayjs(date)
    .utc()
    .format()
}

// 获取8小时之后的时间
time.getEightHoureAfter = function(hour) {
  return dayjs()
    .add(hour, 'hour')
    .utc()
    .format()
}

// 获取8小时之前的时间
time.getEightHoureBefore = function(hour) {
  return dayjs()
    .subtract(hour, 'hour')
    .utc()
    .format()
}

// 获取某天前时间
time.getFourteenDayBefore = function(dayNumber) {
  return dayjs()
    .subtract(dayNumber, 'day')
    .utc()
    .format()
}

// 获取当前时间
time.getCurrectTimeDate = function() {
  return dayjs().format('YYYYMMDDHHmmss')
}
time.getCurrectTimeDateFull = function(time) {
  return dayjs(time).format('YYYY/MM/DD HH:mm:ss')
}
// 获取当前时间
time.getCurrectMDHMTimeDate = function() {
  return dayjs().format('MM-DD HH:mm')
}

// 获取当前时间解析 YYYY-MM-DD
time.TimeDateFormat = function(time) {
  return dayjs(time).format('YYYY/MM/DD HH:mm')
}
time.getCurrectTimeDateFormat = function(time) {
  if (typeof time === 'undefined' || time === null || time === '') {
    return '--'
  }
  return dayjs(time).format('YYYY-MM-DD')
}

// 获取当前时间解析 MM-DD HH:mm
time.getRefreshDateFormat = function(time) {
  if (typeof time === 'undefined' || time === null || time === '') {
    return '--'
  }
  return dayjs(time).format('MM-DD HH:mm')
}

// 获取当前时间解析
time.getCheckChangeDateFormat = function(time, format) {
  return dayjs(time).format(format)
}
// 格式化数字value是字符串数字 num保留的位数
time.getFormatPointNumbers = function(value, num) {
  if (value) {
    value = Number(value)
    return value.toFixed(num)
  } else {
    return ''
  }
}

// 转化为date对象
time.transformAnyToDate = function(value) {
  return value ? dayjs(value).toDate() : ''
}

// 计算剩余时间
time.computedLastTime = function(utcTime) {
  const day = dayjs(utcTime).diff(dayjs(), 'day', false)
  const hour = dayjs(utcTime).diff(dayjs(), 'hour', false)
  const minutes = dayjs(utcTime).diff(dayjs(), 'minutes', false)
  const second = dayjs(utcTime).diff(dayjs(), 'second', false)

  if (day <= 0 && hour <= 0 && minutes <= 0 && second <= 0) {
    return '超时'
  }

  let deadlineString = '剩余'

  if (hour > 0 || day > 0) {
    deadlineString += hour + '小时'
    return deadlineString
  }

  if (minutes > 0) {
    deadlineString += minutes + '分钟'
    return deadlineString
  }

  if (second > 0) {
    deadlineString += second + '秒'
    return deadlineString
  }
}

// 计算剩余时间
time.computedHourMinutes = function(utcTime) {
  const hour = dayjs(utcTime).diff(time.getTodayUTC(), 'hour', false)
  var minutes = dayjs(utcTime).diff(time.getTodayUTC(), 'minutes', false)
  let deadlineString = ''
  // 小时，分都大于0
  if (hour > 0) {
    deadlineString += hour + '小时'
    minutes = minutes - hour * 60
    deadlineString += minutes + '分钟'
    return deadlineString
  }
  // 小时相同，只有分大于0
  if (minutes > 0) {
    deadlineString += minutes + '分钟'
    return deadlineString
  }
  return deadlineString
}

// 比较两个时间大小
time.diffTime = function(t1, t2) {
  const a1 = dayjs(t1).unix()
  const a2 = dayjs(t2).unix()
  const diff = a1 - a2
  return diff
}
// 减7天
time.subtractDay = function(num) {
  const time = dayjs().subtract(num, 'day')
  return time
}
// 加减月份 =》1
time.getOnlyMonth = function(num) {
  const time = dayjs().subtract(num, 'month').format('M')
  return time
}
// 加减年月 =》2021-04
time.getYearMonth = function(num) {
  const time = dayjs().subtract(num, 'month').format('YYYY-MM')
  return time
}

// 以插件的形式提供给Vue
time.install = function(Vue) {
  Vue.prototype.$time = time
}

export default time
